<%@taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<title>Share With Me</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<!--[if IE 6]>
		<link href="css/ie6.css" rel="stylesheet" type="text/css" />
	<![endif]-->
<!--[if IE 7]>
        <link href="css/ie7.css" rel="stylesheet" type="text/css" />  
	<![endif]-->
<style type="text/css">
.happy-button {
	background-image:
		url('http://localhost:6467/ShareWithMe/images/bookworm.jpg');
	height: 100px;
	width: 100px;
}

input.button_add {
	background-image: url('/images/bookworm.jpg'); /* 16px x 16px */
	background-color: transparent; /* make the button transparent */
	background-repeat: no-repeat;
	/* make the background image appear only once */
	background-position: 0px 0px; /* equivalent to 'top left' */
	border: none; /* assuming we don't want any borders */
	cursor: pointer;
	/* make the cursor like hovering over an <a> element */
	height: 100%; /* make this the size of your image */
	width: 100%;
	padding-left: 16px; /* make text start to the right of the image */
	vertical-align: middle; /* align the text vertically centered */
}

	table tr td{
		/* background-color: #F0F0F0; */
	}
	
	.round-box{
		margin-top:10px; 
		border: solid 2px #FFFFFF; 
		padding:10px; 
		-moz-border-radius: 5px;
		border-radius: 5px;
	}
	.right{
		text-align: right;
	}
</style>

<script type="text/javascript">
function submitForm(clickValue){
	var myTextBox = document.getElementById('ShareID');
	myTextBox.value=clickValue;
	formComment.submit();
}
</script>

</head>

<body>
<form action="CommentServlet" method="post" name="formComment">
	<div id="background">
		<div id="page">

			<div class="header">
				<div class="footer">
					<div class="body">

						<!-- include Menu -->
						
						<jsp:include page="Menu.jsp" flush="True">
							<jsp:param name="page_call" value="ShareWithMe" />
						</jsp:include>
						
						<div id="content" ><br>
							<div class="pagecontent">
								<b>Share With Me... แชร์ประสบการณ์การอ่านหนังสือร่วมกับเรา</b>
								<div style="border-bottom: solid white 1px;"></div>
								
								<table border="0" width="100%">
									<tr>
										<sql:query dataSource="jdbc/sharewithme" var="rs">
											select 	bisbn, bnamethai, bnameeng, bwriter, bcountpage,
													bpublishing, bedition,btranslate, bprice 
											FROM  	sharewithme.tbbooks
											where 	bisbn = '${param.bisbn }'
										</sql:query>
										<c:forEach items="${rs.rows }" var="book">
											<th>
												<img src="/ShareWithMe/BookCoverImage?isbn=${param.bisbn}" alt="ภาพปก" width="240" height="350"/>
												<img height="200" width="150">
											</th>
											<th valign="top">
												<div class="round-box" style="background-color: #F0F0F0;">
													<table>
														<tr><td class="right"><h3>ชื่อหนังสือ :</h3></td><td><h3>${book.bnamethai }</h3></td></tr>
														<tr><td class="right">คะแนน :</td><td><jsp:include page="GiveAVote.jsp" /></td></tr>
														<tr><td class="right">จำนวนหน้า :</td><td>${book.bcountpage }</td></tr>
														<tr><td class="right">ราคา :</td><td>${book.bprice }</td></tr>
														<tr><td class="right">ผู้แต่ง :</td><td>${book.bwriter }</td></tr>
														<tr><td class="right">ผู้แปล :</td><td>${book.bpublishing }</td></tr>
													</table>
													<a href="BookDetail.jsp?bisbn=${book.bisbn }">ดูรายละเอียดเพิ่มเติม</a>
												</div>
											</th>
										</c:forEach>
									</tr>
								</table>
								
							</div>
							
							
							<%-- ******** view process ********  --%>
							<sql:query dataSource="jdbc/sharewithme" var="rs">
								select count(*) as isbn from sharewithme.tbtopview where isbn = '${param.bisbn }'
							</sql:query>
							<c:forEach items="${rs.rows }" var="view">

								<c:choose>
									<c:when test="${view.isbn == 0}">

										<sql:update var="rs" dataSource="jdbc/sharewithme">
											insert into sharewithme.tbtopview(isbn, countview, countcomment, countrate) values ('${param.bisbn }',1,0,0)
										</sql:update>
									</c:when>
									<c:otherwise>
										<c:if test="${shareButton != 'Share With You' }">
											<sql:update var="rs" dataSource="jdbc/sharewithme">
												UPDATE ShareWithMe.TBTopView SET CountView= CountView + 1
												where ISBN = '${param.bisbn }'
											</sql:update>
										</c:if>
									</c:otherwise>
								</c:choose>
							</c:forEach>
							
							<%-- ******** view process ********  --%>
							
							<br>
							<div class="pagecontent">
								<h3>Hot comment with me.</h3>							
								
								
								<div class="round-box">
									<b>Good comment</b>
									<div style="border-bottom: solid white 1px;"></div>
									
									<sql:query var="rs" dataSource="jdbc/sharewithme">
									select data.*, tbuser.displayname from sharewithme.tbuser tbuser,(
										select share.* 
										from sharewithme.tbshare share,(
											select max(summary.hotcomment), summary.sharevalue, summary.worm 
											from (
													select count(*) as hotcomment ,tbshare.sharevalue as sharevalue, allcomment.sharevalue as worm 
													from (	select 	no, sharevalue
															FROM 	sharewithme.tbshare
															where 	sharegroupid = '5' 
																	and sharetypeid = 2
											 						and isbn = '${param.bisbn }'
											 						) allcomment, sharewithme.tbshare tbshare
											 		where 	allcomment.no = tbshare.sharevalue
											 				and tbshare.sharetypeid = 1 and allcomment.sharevalue = 1
											 		group by tbshare.sharevalue, allcomment.sharevalue
										 		)summary 
										 	where summary.worm = 1) last
										 where share.no = last.sharevalue) data
              							 where tbuser.userid = data.userid 
									</sql:query> 
									
									<table>
										<c:forEach items="${rs.rows }" var="comment">
											<tr>
												<td style="width:100%">
													<span style="font-size:10px;margin-right:10px;">Ref.#${comment.no }</span>${comment.sharecomment } <br>
													<div style="font-size:10px;">By ${comment.displayname } [${comment.sharedate }]</div>
												</td>
												<td>
													<c:choose>
														<c:when test="${comment.sharevalue == 1}">
																<image src="<%=request.getContextPath()%>/images/happy_worm.png"
																onclick="JavaScript:submitForm(${comment.no })"
																type="image" style="height: 100px; width: 100px;cursor: pointer;" />

																<sql:query dataSource="jdbc/sharewithme" var="rs">
																	select count(*) as countlike 
																	FROM sharewithme.tbshare 
																	where sharegroupid = '${comment.sharegroupid }' and sharetypeid = 1
																		and sharevalue = ${comment.no }  
																		and isbn = '${comment.isbn }'
																</sql:query>
																<c:forEach items="${rs.rows }" var="countlike">
																	countlike : ${countlike.countlike }
																</c:forEach>

															</c:when>

														</c:choose>
													</td>
												</tr>
											</c:forEach>
										</table>
										
										
										<br>
										<b>Bad comment</b>
										<div style="border-bottom: solid white 1px;"></div>
										
										<sql:query var="rs" dataSource="jdbc/sharewithme">
										select data.*, tbuser.displayname from sharewithme.tbuser tbuser,(
											select share.* 
											from sharewithme.tbshare share,(
												select max(summary.hotcomment), summary.sharevalue, summary.worm 
												from (
													select count(*) as hotcomment ,tbshare.sharevalue as sharevalue, allcomment.sharevalue as worm 
													from 
														(select no, sharevalue
														FROM sharewithme.tbshare
														where sharegroupid = '5' 
															and sharetypeid = 2
											 				and isbn = '${param.bisbn }') allcomment, sharewithme.tbshare tbshare
													where allcomment.no = tbshare.sharevalue
														and tbshare.sharetypeid = 1 
														and allcomment.sharevalue = 0
													group by tbshare.sharevalue, allcomment.sharevalue )summary 
												where summary.worm = 0 ) last
											where share.no = last.sharevalue) data
              							 where tbuser.userid = data.userid 
										</sql:query> 
										
											<table>
												<c:forEach items="${rs.rows }" var="comment">
														<tr>
															<td style="width:100%">
																<span style="font-size:10px;margin-right:10px;">Ref.#${comment.no }</span>${comment.sharecomment } <br>
																<div style="font-size:10px;">By ${comment.displayname } [${comment.sharedate }]</div>
															</td>
															<td>
																<c:choose>
																	<c:when test="${comment.sharevalue ==0}">
																		<image
																			src="<%=request.getContextPath()%>/images/angry_worm.png"
																			onclick="JavaScript:submitForm(${comment.no })"
																			type="image"
																			style="height: 100px; width: 100px;cursor: pointer;" />
																		<sql:query dataSource="jdbc/sharewithme" var="rs">
																			select count(*) as countlike FROM sharewithme.tbshare where 
																			sharegroupid = '${comment.sharegroupid }' and sharetypeid = 1
																			and sharevalue = ${comment.no }  and 
																			isbn = '${comment.isbn }'
																		</sql:query>
																		<c:forEach items="${rs.rows }" var="countlike">
																			countlike : ${countlike.countlike }
																		</c:forEach>
																	</c:when>
																</c:choose>
															</td>
															</tr>
															
													</c:forEach>
												</table>	
									</div>
									
								</div>
							
							
							<br>
							<div class="pagecontent">
								<h4>Comment.</h4>	
								<div class="round-box">
								
									<sql:query dataSource="jdbc/sharewithme" var="rs">
										SELECT no, sharegroupid, sharetypeid, sharevalue, sharecomment, 
										tbuser.userid as userid, tbuser.displayname as displayname, isbn, DATE_FORMAT(sharedate, '%d/%m/%Y %H:%i') as sharedate 
										FROM sharewithme.tbshare tbshare
										, sharewithme.tbuser tbuser
										where sharegroupid = 5
										and sharetypeid = 2 and isbn = ${param.bisbn } 
										and tbshare.userid = tbuser.userid
										order by sharedate
									</sql:query>
									
									
								<table>
									
									<c:forEach items="${rs.rows }" var="comment">
										<tr>
											<td style="width:100%">
												<span style="font-size:10px;margin-right:10px;">Ref.#${comment.no }</span>${comment.sharecomment } <br>
												<div style="font-size:10px;">By ${comment.displayname } [${comment.sharedate }]</div>
											</td>
											<td>
												<c:choose>
													<c:when test="${comment.sharevalue == 1}">
														<image src="<%=request.getContextPath()%>/images/happy_worm.png"
																	onclick="JavaScript:submitForm(${comment.no })"
																	type="image" style="height: 100px; width: 100px;cursor: pointer;" />
														<sql:query dataSource="jdbc/sharewithme" var="rs">
															select count(*) as countlike FROM sharewithme.tbshare where 
															sharegroupid = '${comment.sharegroupid }' and sharetypeid = 1
															and sharevalue = ${comment.no }  and 
															isbn = '${comment.isbn }'
														</sql:query>
														
														<c:forEach items="${rs.rows }" var="countlike">
															<br>เห็นด้วย : ${countlike.countlike }
														</c:forEach>
													</c:when>
															
													<c:when test="${comment.sharevalue ==0}">
														<image
															src="<%=request.getContextPath()%>/images/angry_worm.png"
															onclick="JavaScript:submitForm(${comment.no })"
															type="image"
															style="height: 100px; width: 100px;cursor: pointer;" />
		
														<sql:query dataSource="jdbc/sharewithme" var="rs">
															select count(*) as countlike 
															FROM 	sharewithme.tbshare 
															where 	sharegroupid = '${comment.sharegroupid }' and sharetypeid = 1
																	and sharevalue = ${comment.no }  and 
																	isbn = '${comment.isbn }'
														</sql:query>
														<c:forEach items="${rs.rows }" var="countlike">
															<br>เห็นด้วย : ${countlike.countlike }
														</c:forEach>
													</c:when>
												</c:choose>
											</td>
										</tr>
										
										 
										<tr><td colspan="2">
											<div style="border-bottom: solid white 1px;">&nbsp;</div>
										</td></tr>
										
									</c:forEach>
									
								</table>
									
									

								
									<c:choose>
										<c:when test="${sessionScope.User.getDisplayName() != null}">
	
											<table border="0" width="100%">
												<tr>
													<th>
														<h3>Write Comment</h3> 
														<textarea class="text" cols="80" name="comment" rows="5"></textarea> <br> 
														By <input value="${sessionScope.User.getDisplayName() }" name="userId"><br> 
														<input type="radio" name="worm" checked="checked" value="1"  id="worm1">
														<label for="worm1">
															<img src="<%=request.getContextPath()%>/images/happy_worm.png" style="height: 100px; width: 100px;">
														</label>
														
														<input type="radio" name="worm" value="0" id="worm0">
														<label for="worm0">
															<img src="<%=request.getContextPath()%>/images/angry_worm.png" style="height: 100px; width: 100px;">
														</label>
														<br> 
														<input type="submit" value="Share With You" name="shareButton">
													</th>
												</tr>
											</table>
										</c:when>
										<c:otherwise>Please <a href="Index.jsp">Login</a> to Comment Login</c:otherwise>
									</c:choose>										
									
									<input type="hidden" name="isbn" value="${param.bisbn }" >
									<input type="hidden" name="WormButton" id="ShareID" />
									<input type="hidden" name="page_call" value="ShareWithMe">
								
									
								</div>
							</div>
							
						</div>
						

						
						
					</div>
				</div>
				<div class="shadow">&nbsp;</div>
			</div>
		</div>
	</div>
	
	
	</form>
</body>
</html>